package com.atzly.community.controller;

import com.atzly.community.service.AccessCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

/**
 * 统计用户访问情况
 * 1 uv
 * 2 dav
 *
 * @author zlysimida
 * @date 2021/9/4 - 7:31
 */

@Controller
@RequestMapping("/minitor")
public class AccessCountController {

    @Autowired
    private AccessCountService accessService;

    @RequestMapping(value = "/page", method = {RequestMethod.POST, RequestMethod.GET})
    public String accessPage() {
        return "/site/admin/data";
    }


    @PostMapping("/uv")
    public String accessMoneyDaysCountUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                                         @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
                                         Model model) {
        long uvCount = accessService.countManyDaysUv(start, end);
        model.addAttribute("uvCount", uvCount);
        model.addAttribute("uvStartDate", start);
        model.addAttribute("uvEndDate", end);
        // 再次转发到/minitor/page请求，转发是在一个请求中完成的，表示的意思是：我这个方法对处理不完，需要转发到另外，交给你完成后续任务
        return "forward:/minitor/page";
    }

    @PostMapping("/dau")
    public String accessMoneyDaysCountDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                                          @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
                                          Model model) {

        long dauCount = accessService.countManyDaysDau(start, end);
        model.addAttribute("dauCount", dauCount);
        model.addAttribute("dauStartDate", start);
        model.addAttribute("dauEndDate", end);
        return "forward:/minitor/page";
    }

}
